﻿using Serilog.Events;
using Serilog.Formatting.Compact;
using Serilog;

namespace Microsoft.AspNetCore.Builder
{
    /// <summary>
    /// 自定义的扩展方法，注册Serilog
    /// </summary>
    public static class RegisterSerilog
    {
        /// <summary>
        /// 注册Serilog
        /// </summary>
        /// <param name="builder"></param>
        public static void AddSerilog(this WebApplicationBuilder builder) 
        {
            string dateFile = DateTime.Now.ToString("yyyyMMdd");
            Log.Logger = new LoggerConfiguration()
               .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
               .Enrich.FromLogContext()
               .WriteTo.Console(new CompactJsonFormatter())              
               //.WriteTo.MySQL(connectionString: builder.Configuration.GetConnectionString("DbConnectionString"), tableName: "Logs") // 输出到数据库
               .WriteTo.Logger(configure => configure
                   .Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Debug)
                   .WriteTo.File(
                       $"logs/log-debug-{dateFile}.txt",
                       shared: true, // 允许其他进程共享日志文件
                       rollingInterval: RollingInterval.Day,
                       outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}"))
               .WriteTo.Logger(configure => configure
                   .Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Information)
                   .WriteTo.File(
                       $"logs/log-info-{dateFile}.txt",
                       shared: true, 
                       rollingInterval: RollingInterval.Day,
                       outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}"))
               .WriteTo.Logger(configure => configure
                   .Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Error)
                   .WriteTo.File(
                       $"logs/log-error-{dateFile}.txt",
                       shared: true,
                       rollingInterval: RollingInterval.Day,
                       outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}"))
               .WriteTo.File(
                   $"logs/log-total-{dateFile}.txt",
                   shared: true, 
                   rollingInterval: RollingInterval.Day,
                   outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}",
                   restrictedToMinimumLevel: LogEventLevel.Debug)              
               .CreateLogger();
            builder.Host.UseSerilog();


            //builder.Services.AddLogging(builder => {           
            //    Log.Logger = new LoggerConfiguration()
            //    .MinimumLevel.Debug()
            //    .WriteTo.Console()
            //    .WriteTo.File($"f:/temp/selfLog/{dateFile}日志的使用.log",                   
            //       rollingInterval: RollingInterval.Day,
            //       outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}",
            //       fileSizeLimitBytes: 10 * 1024 * 1024,
            //       retainedFileCountLimit: 5,
            //       rollOnFileSizeLimit: true,
            //       shared: true, // // 允许其他进程共享日志文件
            //       flushToDiskInterval: TimeSpan.FromSeconds(1))
            //    .CreateLogger();
            //    builder.AddEventLog();
            //});
        }
    }
}
